import Axios from '@/util/ajax'

const state = {
    isInitialized: false,
    //保存报价所有页面都会用到的一些值 ：如下拉框的列表值等,
    initPageData: {
        supplyTypes: [], // 供应商类型
        supplyGrades:[], // 供应商等级
    },
    showEditCompany: false,
    showContactDialog: false,
    companyId: -1,  // 新增或修改的报价
    contact: {id: -1,
        name: "",
        mobile: "",
        email: "",
        qq: ""},
    selectedContacts: [],
    isView: false,
    isViewContact: false,
    ctype:1,
    contacts:[]

};

// 通过 mutation 去更改 state 里面的值
const mutations = {
    SET_COM_TYPE: (state, data) => {
        state.ctype = data
    },
    Set_Show_Edit_Company: (state, data) => {
        state.showEditCompany = data
    },

    Set_Contacts: (state, data) => {
        state.contacts = data
    },

    Show_Add_Contact: (state) => {
        state.contact = {
            id: -1,
            name: "",
            mobile: "",
            email: "",
            qq: "",
            companyId: state.companyId
        };
        state.showContactDialog = true;
        state.isView = false;
        state.isViewContact = false;
    },

    Show_Edit_Contact: (state, contact) => {
        Object.assign(state.contact, contact);
        state.showContactDialog = true;
        state.isView = false;
        state.isViewContact = false;
    },

    Show_View_Contact: (state, contact) => {
        state.contact = contact;
        state.showContactDialog = true;
        state.isViewContact = true;
    },

    Hide_Contact_Dialog: (state) => {
        state.showContactDialog = false
    },

    Set_Selected_Contact: (state, data) => {
        state.selectedContacts = data
    },
};

// 通过 action 去 commit mutation
const actions = {
    async loadInitPageData({state}) {
        //if(!state.isInitialized){
            state.isInitialized = true;
            let res = await Axios.get('/company/loadInitPageData', {});
            console.log("loadInitPageData: ", res);
            state.initPageData.supplyTypes = res.result.supplyTypes;
            state.initPageData.supplyGrades = res.result.supplyGrades;
        //}
    },

    showEditCompany({state}, companyId) {
        state.showEditCompany = true;
        state.companyId = companyId;
        state.isView = false;
        state.isViewContact = false;
    },

    showViewCompany({commit}, companyId) {
        state.showEditCompany = true;
        state.companyId = companyId;
        state.isView = true;
        state.isViewContact = true;
    },

    findContactsByCompanyId({state, commit}){
        Axios.get(`/company/findContactsByCompanyId/${state.companyId}`).then((res) => {
            console.log("查询联系人返回：", res);
            commit("Set_Contacts", res.result);

        })
    },

    removeContacts({state}){
        state.contacts = state.contacts.filter( it => {
            for(let i = 0; i < state.selectedContacts.length; i++){
                if(state.selectedContacts[i].id === it.id){
                    return false;
                }
            }
            return true;
        });
        console.log("state.contacts", state.contacts);
        state.selectedContacts = [];
    }
};

export default {
    namespaced: true,
    state,
    mutations,
    actions
}
